package cn.com.thinvent.dispatch.service.biz.impl;

import cn.com.thinvent.dispatch.dao.IndicatorListMapper;
import cn.com.thinvent.dispatch.dao.ModelTargetMapper;
import cn.com.thinvent.dispatch.entity.IndicatorList;
import cn.com.thinvent.dispatch.entity.ModelTarget;
import cn.com.thinvent.dispatch.entity.response.PagedGridResult;
import cn.com.thinvent.dispatch.service.biz.IndicatorListService;

import org.springframework.stereotype.Component;


import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Indicatorlist)表服务实现类
 *
 * @author makejava
 * @since 2024-04-10 15:26:27
 */
@Component
public class IndicatorListServiceImpl implements IndicatorListService {
    @Resource
    private IndicatorListMapper indicatorListMapper;
    @Resource
    private ModelTargetMapper modelTargetMapper;

    @Override
    public PagedGridResult<IndicatorList> list(String name, Integer pageNum, Integer pageSize) {
        PagedGridResult<IndicatorList> pagedGridResult = new PagedGridResult<>();
        Integer pageBegin = (pageNum - 1) * pageSize;
        List<IndicatorList> list = indicatorListMapper.list(name, pageBegin, pageSize);
        Long count = indicatorListMapper.count(name);
        pagedGridResult.setRows(list);
        pagedGridResult.setRecords(count);
        pagedGridResult.setTotal((Integer.parseInt(count.toString()) + pageSize - 1 )/ pageSize);
        pagedGridResult.setPage(pageNum);
        return pagedGridResult;
    }

    public IndicatorList saveOrUpdate(IndicatorList indicatorList) {
        Date date = new Date();
        if(indicatorList.getId() == null) {
            indicatorList.setCreateTime(date);
            indicatorList.setUpdateTime(date);
            String indicatorListId = indicatorList.getIndicators() ;
            // 使用 split() 方法按逗号分割字符串
            String[] parts = indicatorListId.split(",");

            // Stream API 将字符串数组转换为 Integer 列表
            List<Integer> list = Arrays.stream(parts)
                    .map(Integer::valueOf)
                    .collect(Collectors.toList());
            // 查询指标列表
            List<ModelTarget> targets = modelTargetMapper.getListByIds(list);
            // 使用Stream API连接所有indicator属性
            String concatenatedIndicators = targets.stream()
                    .map(ModelTarget::getExample) // 获取每个ModelTarget的indicator属性
                    .collect(Collectors.joining(",")); // 使用逗号连接字符串

            // 输出结果
            System.out.println(concatenatedIndicators);


            indicatorList.setIndicators(concatenatedIndicators);
            indicatorListMapper.insert(indicatorList);
        } else {
            indicatorList.setUpdateTime(date);
            indicatorListMapper.update(indicatorList);
        }
        return indicatorList;
    }
    /*
    查询指标集详情

     */

    public String  getIndicators(Integer id) {
        return indicatorListMapper.getIndicators(id);
    }
    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public Integer deleteById(Integer id) {
        return indicatorListMapper.deleteById(id) ;
    }
}
